package com;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/com/ServletLogin")
public class ServletLogin extends HttpServlet {
    private final static int LOGIN_ERROR_PASSWORD=-1;
    private final static int LOGIN_ERROR_NAME=0;
    private final static int LOGIN_SUCCESS=1;
    private final static int Login=1;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DBUtil.BuildConnected();
        String path=this.getServletContext().getRealPath("/WEB-INF/userlist.txt");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        int flag=FileUnit.finduser(username,password,path,Login);
        System.out.println(flag);
        if(flag==LOGIN_SUCCESS)
            request.getRequestDispatcher("/LoginSuccess.jsp").forward(request,response);
        else if(flag==LOGIN_ERROR_NAME){
            request.setAttribute("ERROR","用户名不存在");
            request.getRequestDispatcher("/LoginLoss.jsp").forward(request,response);
        }
        else if(flag==LOGIN_ERROR_PASSWORD){
            request.setAttribute("ERROR","密码错误");
            request.getRequestDispatcher("/LoginLoss.jsp").forward(request,response);
        }
        else
            request.getRequestDispatcher("/LoginLoss.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}